#!/bin/bash

# Copyright (c) 2021-2023 HiHope Open Source Organization .
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
echo "device adl kernel build:"
pushd ${1}
export IMAGE_PATH=${2}
export ROOT_DIR=${5}
export PRODUCT_PATH=${4}
export DEVICE_COMPANY=${6}
export DEVICE_NAME=${7}
export PRODUCT_COMPANY=${8}
export KERNEL_VERSION=linux-5.10
KERNEL_FORM=${9}
KERNEL_PROD=${10}
export BUILD_TYPE=${11}

export KERNEL_SRC_TMP_PATH=${ROOT_DIR}/out/kernel/src_tmp/${KERNEL_VERSION}
export KERNEL_OBJ_TMP_PATH=${ROOT_DIR}/out/kernel/OBJ/${KERNEL_VERSION}
KERNEL_SOURCE=${ROOT_DIR}/kernel/linux/${KERNEL_VERSION}
KERNEL_PATCH_PATH=${ROOT_DIR}/kernel/linux/patches/${KERNEL_VERSION}
KERNEL_PATCH=${ROOT_DIR}/kernel/linux/patches/${KERNEL_VERSION}/adl_patch/adl.patch
BUILD_SCRIPT_PATH=${3}
KERNEL_MAKE_FILE=${BUILD_SCRIPT_PATH}/kernel/kernel.mk
# NEWIP_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/newip/apply_newip.sh
#XPM_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/xpm/apply_xpm.sh
#CODE_SIGN_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/code_sign/apply_code_sign.sh
# QOS_AUTH_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/qos_auth/apply_qos_auth.sh
export KERNEL_IMAGE="bzImage"
LINUX_KERNEL_IMAGE_FILE=${KERNEL_OBJ_TMP_PATH}/arch/x86/boot/${KERNEL_IMAGE}

HARMONY_CONFIG_PATH=${ROOT_DIR}/kernel/linux/config/${KERNEL_VERSION}
DEVICE_CONFIG_PATH=${ROOT_DIR}/kernel/linux/config/${KERNEL_VERSION}/${DEVICE_NAME}
#DEFCONFIG_BASE_FILE=${HARMONY_CONFIG_PATH}/base_defconfig
#DEFCONFIG_TYPE_FILE=${HARMONY_CONFIG_PATH}/type/standard_defconfig
#DEFCONFIG_FORM_FILE=${HARMONY_CONFIG_PATH}/form/${KERNEL_FORM}_defconfig
export DEVICE_DEFCONFIG=${DEVICE_NAME}_${BUILD_TYPE}_defconfig
DEFCONFIG_ARCH_FILE=${HARMONY_CONFIG_PATH}/arch/x86/configs/${DEVICE_DEFCONFIG}
#DEFCONFIG_PROC_FILE=${DEVICE_CONFIG_PATH}/product/${KERNEL_PROD}_defconfig

#RAMDISK_ARG="disable_ramdisk"
#MAKE_OHOS_ENV="GPUDRIVER=mali"
export KBUILD_OUTPUT=${KERNEL_OBJ_TMP_PATH}

source ${BUILD_SCRIPT_PATH}/kernel/kernel_source_checker.sh

for i in "$@"
do
    case $i in
        enable_ramdisk)
            RAMDISK_ARG=enable_ramdisk
            ;;
        enable_mesa3d)
            MAKE_OHOS_ENV="GPUDRIVER=mesa3d"
            ;;
    esac
done

function copy_and_patch_kernel_source()
{
    rm -rf ${KERNEL_SRC_TMP_PATH}
    mkdir -p ${KERNEL_SRC_TMP_PATH}

    rm -rf ${KERNEL_OBJ_TMP_PATH}
    mkdir -p ${KERNEL_OBJ_TMP_PATH}

    cp -arf ${KERNEL_SOURCE}/* ${KERNEL_SRC_TMP_PATH}/

    cd ${KERNEL_SRC_TMP_PATH}

    #HDF patch
    echo "adl build_kernel: HDF patch DEVICE_NAME: ${DEVICE_NAME}"
    bash ${ROOT_DIR}/drivers/hdf_core/adapter/khdf/linux/patch_hdf.sh ${ROOT_DIR} ${KERNEL_SRC_TMP_PATH} ${KERNEL_PATCH_PATH} ${DEVICE_NAME}

    echo "adl build_kernel: kernel patch"
    #kernel patch
    patch -p1 < ${KERNEL_PATCH}

    #newip
#    if [ -f $NEWIP_PATCH_FILE ]; then
#        bash $NEWIP_PATCH_FILE ${ROOT_DIR} ${KERNEL_SRC_TMP_PATH} ${DEVICE_NAME} ${KERNEL_VERSION}
#    fi

#    echo "adl build_kernel: xpm patch file: ${XPM_PATCH_FILE}"
    #xpm
#    if [ -f $XPM_PATCH_FILE ]; then
#        bash $XPM_PATCH_FILE ${ROOT_DIR} ${KERNEL_SRC_TMP_PATH} ${DEVICE_NAME} ${KERNEL_VERSION}
#    fi

#    echo "adl build_kernel: code_sign patch file: ${CODE_SIGN_PATCH_FILE}"
    #code_sign
#    if [ -f $CODE_SIGN_PATCH_FILE ]; then
#        bash $CODE_SIGN_PATCH_FILE ${ROOT_DIR} ${KERNEL_SRC_TMP_PATH} ${DEVICE_NAME} ${KERNEL_VERSION}
#    fi

#    #qos_auth
#    if [ -f $QOS_AUTH_PATCH_FILE ]; then
#        bash $QOS_AUTH_PATCH_FILE ${ROOT_DIR} ${KERNEL_SRC_TMP_PATH} ${DEVICE_NAME} ${KERNEL_VERSION}
#    fi

    echo "cp logo"
    cp -rf ${BUILD_SCRIPT_PATH}/kernel/logo* ${KERNEL_SRC_TMP_PATH}/

    #config
    if [ ! -f "$DEFCONFIG_FORM_FILE" ]; then
        DEFCONFIG_FORM_FILE=
        echo "warning no form config file $(DEFCONFIG_FORM_FILE)"
    fi
    if [ ! -f "$DEFCONFIG_PROC_FILE" ]; then
        DEFCONFIG_PROC_FILE=
        echo "warning no prod config file $(DEFCONFIG_PROC_FILE)"
    fi
    echo "start merge_config def: ${DEFCONFIG_TYPE_FILE} from:${DEFCONFIG_FROM_FILE} arch: ${DEFCONFIG_ARCH_FILE} PROC:${DEFCONFIG_PROC_TILE} BASE:${DEFCONFIG_BASE_FILE}"
#    bash ${KERNEL_SRC_TMP_PATH}/scripts/kconfig/merge_config.sh -O ${KERNEL_SRC_TMP_PATH}/arch/x86/configs/ -m ${DEFCONFIG_ARCH_FILE} ${DEFCONFIG_TYPE_FILE} ${DEFCONFIG_FORM_FILE} ${DEFCONFIG_BASE_FILE} ${DEFCONFIG_PROC_FILE}
#    mv ${KERNEL_SRC_TMP_PATH}/arch/x86/configs/.config ${KERNEL_SRC_TMP_PATH}/arch/x86/configs/${DEVICE_DEFCONFIG}
    cp ${DEFCONFIG_ARCH_FILE} ${KERNEL_SRC_TMP_PATH}/arch/x86/configs/${DEVICE_DEFCONFIG}

    #selinux config patch
#    for arg in "$@"; do
#        if [ "$arg" = "is_release" ]; then
#            echo "close selinux kernel config CONFIG_SECURITY_SELINUX_DEVELOP in release version"
#            ${KERNEL_SOURCE}/scripts/config --file ${KERNEL_SRC_TMP_PATH}/arch/x86/configs/adl_standard_defconfig -d SECURITY_SELINUX_DEVELOP
#        fi
#    done

#    if [ $MAKE_OHOS_ENV == "GPUDRIVER=mesa3d" ]; then
#        python ${ROOT_DIR}/third_party/mesa3d/ohos/modifyDtsi.py ${KERNEL_SRC_TMP_PATH}/arch/arm64/boot/dts/rockchip/rk3568.dtsi
#    fi
}

set +e
is_kernel_change ${ROOT_DIR}
KERNEL_SOURCE_CHANGED=$?
set -e
echo "adl build_kernel start >>>"
if [ ${KERNEL_SOURCE_CHANGED}  -ne 0 ]; then
    echo "kernel or it's deps changed, start source update."
    copy_and_patch_kernel_source
else
    echo "no changes to kernel, skip source copy."
fi

#cd ${KERNEL_SRC_TMP_PATH}
echo "start make kernel.mk"
make -f ${KERNEL_MAKE_FILE}

if [ -f "${LINUX_KERNEL_IMAGE_FILE}" ]; then
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build success"
    #cp ${LINUX_KERNEL_IMAGE_FILE} ${IMAGE_PATH}/
else
    echo "Image: ${LINUX_KERNEL_IMAGE_FILE} build failed!!!"
    exit 1
fi

popd

if [ ${KERNEL_SOURCE_CHANGED} -ne 0 ]; then
    cp ${ROOT_DIR}/out/kernel/checkpoint/last_build.info ${ROOT_DIR}/out/kernel/checkpoint/last_build.backup
    cp ${ROOT_DIR}/out/kernel/checkpoint/current_build.info ${ROOT_DIR}/out/kernel/checkpoint/last_build.info
    echo "kernel compile finish, save build info."
else
    echo "kernel compile finish."
fi
